locals {
  cce_node_pools = {
    cce-myapp = {
      cluster_id = data.terraform_remote_state.cce_cluster.outputs.cce_info["myapp"].cluster_id
      node_name  = ["node-myapp001", "node-myapp002", "node-myapp003"]
      os         = "Ubuntu 22.04 server 64bit"
      flavor_id  = data.huaweicloud_compute_flavors.ecs_4c8g.ids[0]
      // flavor_id                = "c6.2xlarge.2"
      availability_zone = "${var.REGION}c"
      key_pair          = data.terraform_remote_state.keypair.outputs.keypairs_info.id
      root_volume_size  = 40
      root_volume_type  = "SSD"
      data_volume_size  = 100
      data_volume_type  = "SSD"
    }
  }
}

locals {
  flattened_node_pools = flatten([
    for name, conf in local.cce_node_pools : [
      for i, node_name in conf.node_name : {
        name              = "${name}-${node_name}"
        cluster_id        = conf.cluster_id
        node_name         = conf.node_name
        os                = conf.os
        flavor_id         = conf.flavor_id
        availability_zone = conf.availability_zone
        key_pair          = conf.key_pair
        root_volume_size  = conf.root_volume_size
        root_volume_type  = conf.root_volume_type
        data_volume_size  = conf.data_volume_size
        data_volume_type  = conf.data_volume_type

      }
    ]
  ])
}

module "cce-nodes" {
  source            = "../../../modules/cce-nodes"
  for_each          = { for np in local.flattened_node_pools : np.name => np }
  cluster_id        = each.value.cluster_id
  name              = each.value.name
  os                = each.value.os
  flavor_id         = each.value.flavor_id
  availability_zone = each.value.availability_zone
  key_pair          = each.value.key_pair
  root_volume_size  = each.value.root_volume_size
  root_volume_type  = each.value.root_volume_type
  data_volume_size  = each.value.data_volume_size
  data_volume_type  = each.value.data_volume_type
}